package com.example.graduatetest.adapter;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.example.graduatetest.R;
import com.example.graduatetest.activitys.ClassDetailActivity;
import com.example.graduatetest.activitys.NoticeDetailActivity;
import com.example.graduatetest.bean.Notice;
import com.example.graduatetest.view.model.NoticeDetailViewModel;

import org.w3c.dom.Text;

import java.util.List;

public class NoticeAdapter extends RecyclerView.Adapter<NoticeAdapter.ViewHolder> {
    private List<Notice> noticeList;
    private Activity activity;
    static class ViewHolder extends RecyclerView.ViewHolder{
        TextView text_title;
        TextView text_send_person;
        TextView text_send_time;
        TextView text_content;
        TextView text_label;
        CardView cardView;
        ViewHolder(@NonNull View itemView) {
            super(itemView);
            text_title = (TextView)itemView.findViewById(R.id.text_title);
            text_send_person = (TextView)itemView.findViewById(R.id.text_send_person);
            text_send_time = (TextView)itemView.findViewById(R.id.text_send_time);
            text_content = (TextView)itemView.findViewById(R.id.text_content);
            text_label = (TextView)itemView.findViewById(R.id.text_label);
            cardView = (CardView)itemView.findViewById(R.id.card_notice);
        }
    }

    public NoticeAdapter(List<Notice> noticeList, Activity activity){
        this.noticeList = noticeList;
        this.activity = activity;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.notice_item,parent,false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        final Notice notice = noticeList.get(position);
        holder.text_title.setText(notice.getTitle());
        holder.text_label.setText("通知类型："+notice.getLabel());
        holder.text_send_person.setText(notice.getSend_name());
        holder.text_send_time.setText("发布时间："+notice.getTime());
        holder.text_content.setText(notice.getContent());
        holder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(activity.getApplication(), NoticeDetailActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra("notice", notice);
                activity.getApplication().startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return noticeList.size();
    }
}
